<script lang="ts">
export default {
  name: "ShortTermAssistant",
};
</script>
<template>
  <div class="page-box h100 display-flex czbj">
    <span>
      <n-button type="primary" @click="showModal = true">添加指标</n-button>
    </span>
    <n-spin :show="tableLoad">
      <n-data-table
        remote
        size="small"
        class="mt10"
        :row-key="(row) => row.symbol"
        :row-class-name="rowClassName"
        :pagination="paginationReactive"
        :columns="columns"
        :single-line="false"
        :data="Tabledata"
        :row-props="rowProps"
        @update:sorter="handleSorterChange"
      />
    </n-spin>
    <n-modal
      v-model:show="showModal"
      preset="card"
      class="settlement-condition"
    >
      <template #header>
        <div>条件设置</div>
      </template>
      <settlementCondition ref="stion"></settlementCondition>

      <template #action>
        <div class="tr">
          <n-button type="info" ghost @click="showModal = false">
            取消
          </n-button>
          <n-button
            type="info"
            class="ml10"
            @click="callData(false)"
            :loading="tableLoad"
          >
            确认
          </n-button>
        </div>
      </template>
    </n-modal>
  </div>
</template>
 <script lang='ts' setup>
import { h, ref, reactive } from "vue";
import comp from "../../components/index";
import FormTemplate from "../../components/details/FormTemplate";
import settlementCondition from "../../components/util/settlementCondition.vue";
import { dxget, hf } from "../../util";
import { listData, listDataer, listDatasan } from "../../util/xgData";
let tabsValue = ref("1");
let FormTemplateRef = ref();
const showModal = ref(false);
let stion = ref();
let Tabledata = ref([]);
let order_by = "pct";
let order = "desc";
const tableLoad = ref(false);
const sdweedata = [...listData, ...listDataer, ...listDatasan];

const paginationReactive = reactive({
  page: 1,
  itemCount: 0,
  pageSize: 20,
  onChange: (page) => {
    paginationReactive.page = page;
    callData(true);
  },
});

const rowClassName = (row, index) => {
  if (row.pct >= 0) {
    return "red";
  }
  return "ls";
};
let ewdata = [];
let colRight = [
  {
    title: "操作",
    align: "center",
    key: "actions",
    render(row) {
      row.secCode = row.f12;
      return h(comp.MoreOperations, {
        obj: [
          {
            label: "选股器",
            key: "16",
          },
          {
            label: "短线助手",
            key: "10",
          },
        ],
        rowData: row,
      });
    },
  },
];
let columns = ref();

const tabupdate = (num) => {
  tabsValue.value = num;
  FormTemplateRef.value.dx(num);
};
const rowProps = (row, aa) => {
  return {
    style: "cursor: pointer;",
    onClick: (idx) => {
      hf("stockDetails", {
        code: row.f12,
        // observetype: props.observetype,
      });
    },
  };
};

const callData = async (tr) => {
  tableLoad.value = true;
  let cs: any = {};
  let formList = [];
  if (tr && localStorage.formList) {
    formList = JSON.parse(localStorage.formList);
  } else {
    formList = stion.value.getData();
    console.log(formList);
  }

  let obj = {};
  formList.map((a) => {
    if (a.year) {
      obj[a.key + "." + a.year + "" + a.btype] = a.minVal + "_" + a.maxVal;
    } else {
      obj[a.key] = a.minVal + "_" + a.maxVal;
    }
  });
  cs = obj;

  cs.order_by = order_by;
  cs.order = order;
  cs.page = paginationReactive.page;
  cs.size = paginationReactive.pageSize;
  let { data }: any = await dxget("xq/xgq", cs);
  columns.value = [
    {
      title: "股票号码",
      key: "f12",
      align: "center",
    },
    {
      title: "股票名称",
      key: "name",
      align: "center",
      fixed: "left",
    },
    {
      title: "当前价",
      key: "current",
      align: "center",
      render(row) {
        return row.current.toFixed(2);
      },
    },
  ];

  paginationReactive.itemCount = data.count;
  Tabledata.value = data.list;
  showModal.value = false;
  console.log(formList);
  formList.map((a) => {
    const objse: any = sdweedata.filter((f: any) => a.key == f.key)[0];
    objse.title = objse.name;
    (objse.align = "center"), columns.value.push(objse);
  });
  tableLoad.value = false;
  columns.value = [...columns.value, ...colRight];
};

callData(true);
const handleSorterChange = (e) => {
  order_by = e.columnKey;
  if (e.order == "descend") {
    order = "desc";
  } else {
    order = "asc";
  }
  callData(true);
  console.log(e);
};
</script>
 <style scoped>
</style>